#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>

int main()
{
    int pipefd[2];
    char buffer[47];
    pid_t pid;
    if(pipe(pipefd)==-1)
    {
        perror("pipe fail");
        return 1;
    }
    //创建子进程
    pid = fork();
    if(pid == -1)
    {
        perror("fork fail");
        return 1;
    }
    if(pid == 0)
    {
        //子进程
        close(pipefd[1]);//关闭写功能
        //从管道读取数据
        read(pipefd[0],buffer,sizeof(buffer));
        printf("子进程接收到的数据:%s\n",buffer);
        close(pipefd[0]);
    }
    else{
        //父进程
        close(pipefd[0]);//关闭读功能
        write(pipefd[1],"落霞与孤鹜齐飞，秋水共长天一色",46);
        close(pipefd[1]);
    }
    return 0;
}